// 初始化增量字段下拉
function initConditionColumn($this, columns){
	var colLen = columns.length;
	var opts = "";
	for (var i = 0; i < colLen; i++) {
		var col = columns[i];
		var name = col['name'];
		var type = col['type'];
		var typeName = col['typeName'];
		opts += "<option value='"+ name +"' type='"+ type +"' typeName='"+ typeName +"' >"+ name +" ("+typeName+")</option>";
	}
	$this.html(opts);
}

$(function() {
	// 初始化增量字段和事件字段
	var $sourceStrategyQuartzCron = $("#sourceStrategyQuartzCron");
	var $sourceStrategyQuartzFiled = $("#sourceStrategyQuartzFiled");
	var $sourceStrategyQuartzEvent = $("#sourceStrategyQuartzEvent");
	var $sourceStrategyQuartzEventInsert = $("#sourceStrategyQuartzEventInsert");
	var $sourceStrategyQuartzEventUpdate = $("#sourceStrategyQuartzEventUpdate");
	var $sourceStrategyQuartzEventDelete = $("#sourceStrategyQuartzEventDelete");
	var $sourceStrategyIncrementTableLabel = $("#sourceStrategyIncrementTableLabel");
	var $sourceStrategyQuartzScnPos = $("#sourceStrategyQuartzScnPos");
	
	var sourceMapping = driver.sourceMapping;
	var connector = sourceMapping.connector;
	// 如果是sql_mysql/sql_oracle类型的连接器,显示表别名配置输入框
	if(connector == ConnectorConstantSqlMysql || connector == ConnectorConstantSqlOracle){
		$("#sourceStrategyIncrementTableLabelDiv").removeClass("hidden");
		// 添加必填校验
		$sourceStrategyIncrementTableLabel.attr("asd-valid", "require");
	}
	// 初始化数据源字段
	var cols = sourceMapping.config.column;
	initConditionColumn($sourceStrategyQuartzFiled, cols);
	initConditionColumn($sourceStrategyQuartzEvent, cols);
	// 回显读取范围
	var inc = driver.policy.increment;
	if (inc) {
		if(undefined!=inc.quartzCron && ''!=inc.quartzCron){
			$sourceStrategyQuartzCron.val(inc.quartzCron);
		}
		if(undefined!=inc.quartzFiled && ''!=inc.quartzFiled){
			$sourceStrategyQuartzFiled.find("option[value='"+inc.quartzFiled+"']").attr("selected","selected");
		}else {
			$sourceStrategyQuartzFiled.find("option[type='93']").attr("selected","selected");
		}
		$sourceStrategyQuartzEvent.find("option[value='"+inc.quartzEvent+"']").attr("selected","selected");
		if(undefined!=inc.quartzEventInsert && ''!=inc.quartzEventInsert){
			$sourceStrategyQuartzEventInsert.val(inc.quartzEventInsert);
		}
		if(undefined!=inc.quartzEventUpdate && ''!=inc.quartzEventUpdate){
			$sourceStrategyQuartzEventUpdate.val(inc.quartzEventUpdate);
		}
		if(undefined!=inc.quartzEventDelete && ''!=inc.quartzEventDelete){
			$sourceStrategyQuartzEventDelete.val(inc.quartzEventDelete);
		}
		if(undefined!=inc.tableLabel && ''!=inc.tableLabel && 'null'!=inc.tableLabel){
			$sourceStrategyIncrementTableLabel.val(inc.tableLabel);
		}
		if(undefined!=inc.scnPos && ''!=inc.scnPos && 'null'!=inc.scnPos){
			$sourceStrategyQuartzScnPos.val(inc.scnPos);
		}
	}
	
	// 初始化select2插件
	$(".select-control").select2({
		width : "100%",
		theme : "classic"
	});
});